import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/Register.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/Dashboard.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems',
      name: 'Problems',
      component: () => import('@/views/Problems.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/create',
      name: 'CreateProblem',
      component: () => import('@/views/Problems.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/problems/:id',
      name: 'ProblemDetail',
      component: () => import('@/views/ProblemDetail.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/choice/:id',
      name: 'ChoiceProblem',
      component: () => import('@/views/ChoiceProblem.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/blank/:id',
      name: 'BlankProblem',
      component: () => import('@/views/BlankProblem.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/short/:id',
      name: 'ShortAnswerProblem',
      component: () => import('@/views/ShortAnswerProblem.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/code/:id',
      name: 'CodeProblem',
      component: () => import('@/views/ProblemDetail.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/problems/code_completion/:id',
      name: 'CodeCompletionProblem',
      component: () => import('@/views/CodeCompletionProblem.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/homeworks',
      name: 'Homeworks',
      component: () => import('@/views/Homeworks.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/student-homeworks',
      name: 'StudentHomeworks',
      component: () => import('@/views/StudentAssignments.vue'),
      meta: { requiresAuth: true, requiresStudent: true }
    },
    {
      path: '/homeworks/:id',
      name: 'HomeworkDetail',
      component: () => import('@/views/HomeworkDetail.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/submissions',
      name: 'Submissions',
      component: () => import('@/views/Submissions.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/courses',
      name: 'Courses',
      component: () => import('@/views/Courses.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/courses/create',
      name: 'CreateCourse',
      component: () => import('@/views/Courses.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/classes',
      name: 'Classes',
      component: () => import('@/views/Classes.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/student-class',
      name: 'StudentClass',
      component: () => import('@/views/StudentClass.vue'),
      meta: { requiresAuth: true, requiresStudent: true }
    },
    {
      path: '/classes/create',
      name: 'CreateClass',
      component: () => import('@/views/Classes.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/assignments',
      name: 'Assignments',
      component: () => import('@/views/Assignments.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/assignments/create',
      name: 'CreateAssignment',
      component: () => import('@/views/Assignments.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/student-assignments',
      name: 'StudentAssignments',
      component: () => import('@/views/StudentAssignments.vue'),
      meta: { requiresAuth: true, requiresStudent: true }
    },
          {
      path: '/assistant',
      name: 'AIAssistant',
      component: () => import('@/views/AIAssistant.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/ai-problem-generator',
      name: 'AIProblemGenerator',
      component: () => import('@/views/AIProblemGenerator.vue'),
      meta: { requiresAuth: true, requiresTeacher: true }
    },
  {
    path: '/ai-assistant',
    name: 'AIAssistantPage',
    component: () => import('@/views/AIAssistant.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/test-ai',
    name: 'TestAI',
    component: () => import('@/views/TestAI.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/test-chat',
    name: 'TestChat',
    component: () => import('@/views/TestChat.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/test-problems',
    name: 'TestProblems',
    component: () => import('@/views/TestProblems.vue'),
    meta: { requiresAuth: true }
  },

  {
    path: '/student-management',
    name: 'StudentManagement',
    component: () => import('@/views/Students.vue'),
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/students',
    name: 'Students',
    component: () => import('@/views/Students.vue'),
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/teacher-management',
    name: 'TeacherManagement',
    component: () => import('@/views/TeacherManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/simple-chat',
    name: 'SimpleChat',
    component: () => import('@/views/SimpleChat.vue'),
    meta: { requiresAuth: true }
  },
    {
      path: '/admin',
      name: 'Admin',
      component: () => import('@/views/Admin.vue'),
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/profile',
      name: 'Profile',
      component: () => import('@/views/Profile.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/system-logs',
      name: 'SystemLogs',
      component: () => import('@/views/SystemLogs.vue'),
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/async-judge-demo',
      name: 'AsyncJudgeDemo',
      component: () => import('@/views/AsyncJudgeDemo.vue'),
      meta: { requiresAuth: true, roles: ['student', 'teacher', 'admin'] }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 初始化认证状态
  if (!authStore.isLoggedIn()) {
    authStore.initAuth()
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !authStore.isLoggedIn()) {
    next('/login')
    return
  }
  
  // 检查管理员权限
  if (to.meta.requiresAdmin && !authStore.hasRole('admin')) {
    next('/dashboard')
    return
  }
  
  // 检查教师权限
  if (to.meta.requiresTeacher && !authStore.hasRole('teacher') && !authStore.hasRole('admin')) {
    console.log('权限检查失败: 需要teacher或admin权限，当前用户角色:', authStore.user?.role)
    next('/dashboard')
    return
  }
  
  // 检查学生权限
  if (to.meta.requiresStudent && !authStore.hasRole('student')) {
    next('/dashboard')
    return
  }
  
  // 已登录用户访问登录页面，重定向到首页
  if (to.path === '/login' && authStore.isLoggedIn()) {
    next('/dashboard')
    return
  }
  
  next()
})

export default router
